package com.example.fbi_text.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.fbi_text.R;
import com.example.fbi_text.bean.ProductCart;
import com.example.fbi_text.database.DBHelper;

import java.util.List;

public class CartAdapter extends RecyclerView.Adapter<CartAdapter.CartViewHolder> {
    private final Context context;
    private final List<ProductCart> cartItems;
    private int totalCost = 0;
    private TextView totalPriceView;
    private DBHelper dbHelper;

    public CartAdapter(Context context, List<ProductCart> cartItems, TextView totalPriceView, DBHelper dbHelper) {
        this.context = context;
        this.cartItems = cartItems;
        this.totalPriceView = totalPriceView;
        this.dbHelper = dbHelper;
        calculateTotalCost();
    }

    public int getTotalCost() {
        return totalCost;
    }
    @NonNull
    @Override
    public CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_cart, parent, false);
        return new CartViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CartViewHolder holder, int position) {
        ProductCart item = cartItems.get(position);
        holder.cartName.setText(item.getCartName());
        holder.cartPrice.setText("¥" + item.getCartPrice());
        holder.cartDescription.setText(item.getCartDescription());
        holder.quantityText.setText(String.valueOf(item.getQuantity()));

        holder.btnIncrease.setOnClickListener(v -> {
            item.increaseQuantity();
            dbHelper.updateCartItemQuantity(item.getCartID(), item.getQuantity());
            holder.quantityText.setText(String.valueOf(item.getQuantity()));
            updatePriceDisplay(item, holder);
            calculateTotalCost();
        });

        holder.btnDecrease.setOnClickListener(v -> {
            item.decreaseQuantity();
            dbHelper.updateCartItemQuantity(item.getCartID(), item.getQuantity());
            holder.quantityText.setText(String.valueOf(item.getQuantity()));
            updatePriceDisplay(item, holder);
            calculateTotalCost();
        });
    }
    private void updatePriceDisplay(ProductCart item, CartViewHolder holder) {
        holder.cartPrice.setText("¥" + item.getCartPrice());
    }

    @Override
    public int getItemCount() {
        return cartItems.size();
    }

    // 计算总价并更新UI
    private void calculateTotalCost() {
        totalCost = 0;
        for (ProductCart item : cartItems) {
            try {
                totalCost += Integer.parseInt(item.getCartPrice()) * item.getQuantity();
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        if (totalPriceView != null) {
            totalPriceView.setText("¥" + totalCost);
        }
    }

    static class CartViewHolder extends RecyclerView.ViewHolder {
        TextView cartName;
        TextView cartPrice;
        TextView cartDescription;
        TextView quantityText;
        Button btnIncrease;
        Button btnDecrease;

        public CartViewHolder(@NonNull View itemView) {
            super(itemView);
            cartName = itemView.findViewById(R.id.cart_item_name);
            cartPrice = itemView.findViewById(R.id.cart_item_price);
            cartDescription = itemView.findViewById(R.id.cart_item_description);
            quantityText = itemView.findViewById(R.id.tv_quantity);
            btnIncrease = itemView.findViewById(R.id.btn_increase);
            btnDecrease = itemView.findViewById(R.id.btn_decrease);
        }
    }
}